<?php

if( ! defined('DOCROOT') )
{
    require_once '/var/www/advancelab/chilik2/unittest/bootstrap.php';
}

class RouterTest extends UnitTestCase
{
    function testingSimple()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'entry/list';
        
        $router = new testSimpleRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'list' ) , $controller->getVars() );
        
        
        /* Test 2 */
        $request = new Core\MVCRequest;
        $request->uri = 'entry/get/89';
        $router = new testSimpleRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'show', 'id' => '89' ) , $controller->getVars() );
        
        /* Test 3 */
        $request = new Core\MVCRequest;
        $request->uri = 'entry/89';
        $router = new testSimpleRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'show', 'id' => '89' ) , $controller->getVars() );
        
        /* $this->dump($controller); */
    }
    
    function testingInvalidAction()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'test/no-action';
        $router = new testSimpleRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        $controller = new $controllerClass( $routeInfo->action, $request );
        
        try
        {
        
            $controller->createResult();
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( 'Abstraction\MvcController: Action of nonExistingMethod has not found !' , $e->getMessage() );    
        }
        
    }
    
    function testingNoRoutes()
    {
        $request = new Core\MVCRequest;
        $router = new testNoRoutesRouter( $request );
        
        try
        {
            $routeInfo = $router->getRoutingInformation();
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\Router: No Router found !", $e->getMessage() );
        }
    }
    
    function testingNoRoutesMatch()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'nomatch';
        $router = new testSimpleRouter( $request );
        
        try
        {
            $routeInfo = $router->getRoutingInformation();
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\Router: No Router found for this request \"nomatch\" !", $e->getMessage() );
        }
    }
    
    function testingNoRouteMatchedRequestId()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'nomatch';
        $request->requestId = 'localhost/nomatch';
        
        $router = new testSimpleRouter( $request );
        
        try
        {
            $routeInfo = $router->getRoutingInformation();
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\Router: No Router found for this request \"localhost/nomatch\" !", $e->getMessage() );
        }
    }
    
    function testingFaultyRoute()
    {
        $request = new Core\MVCRequest;
        $router = new testFaultyRouteRouter( $request );
        
        try
        {
            $routeInfo = $router->getRoutingInformation();
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "The value [ O:8:\"stdClass\":0:{} ] that you were trying to assign to setting \"route\" is invalid Allowed values are: instance of Base\\MVCRouter.", $e->getMessage() );
        }
    }
    
    function testingPrefixRoute()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'blog/entry/list';
        
        $router = new testPrefixRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'list' ) , $controller->getVars() );
        
        
        /* Test 2 */
        $request = new Core\MVCRequest;
        $request->uri = 'blog/entry/get/89';
        $router = new testPrefixRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'show', 'id' => '89' ) , $controller->getVars() );
        
        /* Test 3 */
        $request = new Core\MVCRequest;
        $request->uri = 'blog/entry/89';
        $router = new testPrefixRouter( $request );
        $routeInfo = $router->getRoutingInformation();
        $controllerClass = $routeInfo->controllerClass;
        $controller = new $controllerClass( $routeInfo->action, $request );
        $this->assertEqual( array( 'method' => 'show', 'id' => '89' ) , $controller->getVars() );
        
        /* $this->dump($controller); */
    }
    
    function testingNoNamedRoute()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'entry/get/89';
        $router = new testNamedRouter( $request );
        
        try
        {
            $routeInfo = $router->generateUrl('list');
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\Router: list MVC Router has not found !", $e->getMessage() );
        }
    }
    
    function testingNamedRouteThatDoesNotSupportReversedRoute()
    {
        $request = new Core\MVCRequest;
        $request->uri = 'entry/get/89';
        $router = new testNamedRouter( $request );
        
        try
        {
            $routeInfo = $router->generateUrl('catchall');
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\Router: catchall MVC Router is not reversable ( Implementation\CatchAllRouter ) !", $e->getMessage() );
        }
    }
    
}
